<?php
@set_time_limit(0);

function makeThumbnail($o_file, $width, $height) {
   $image_info = getImageSize($o_file) ; // see EXIF for faster way
  
   switch ($image_info['mime']) {
       case 'image/gif':
           if (imagetypes() & IMG_GIF)  { // not the same as IMAGETYPE
               $o_im = imageCreateFromGIF($o_file) ;
           } else {
               $ermsg = 'GIF images are not supported<br />';
           }
           break;
       case 'image/jpeg':
           if (imagetypes() & IMG_JPG)  {
               $o_im = imageCreateFromJPEG($o_file) ;
           } else {
               $ermsg = 'JPEG images are not supported<br />';
           }
           break;
       case 'image/png':
           if (imagetypes() & IMG_PNG)  {
               $o_im = imageCreateFromPNG($o_file) ;
           } else {
               $ermsg = 'PNG images are not supported<br />';
           }
           break;
       case 'image/wbmp':
           if (imagetypes() & IMG_WBMP)  {
               $o_im = imageCreateFromWBMP($o_file) ;
           } else {
               $ermsg = 'WBMP images are not supported<br />';
           }
           break;
       default:
           $ermsg = $image_info['mime'].' images are not supported<br />';
           break;
   }
  
   if (!isset($ermsg)) {
       $o_wd = imagesx($o_im) ;
       $o_ht = imagesy($o_im) ;
   
   	$prop=$o_wd/$o_ht;
   
       
      
	  	if ($prop>=1)
		{
			$height=$width/$prop;
		} else {
			$width=$prop*$height;
		}
		$t_im = imageCreateTrueColor($width,$height);		
       imageCopyResampled($t_im, $o_im, 0, 0, 0, 0, $width, $height, $o_wd, $o_ht);
      
       imageJPEG($t_im,"thumbs/".$o_file,100);
      
       imageDestroy($o_im);
       imageDestroy($t_im);
   }
   return isset($ermsg)?$ermsg:NULL;
}



if(!file_exists("thumbs")) mkdir("thumbs");


if(!file_exists("thumbs/".$_GET["url"])){
	//header("Content-type: image/jpeg");
	$tam=$_GET["tam"];
	makeThumbnail($_GET["url"], $tam, $tam);
}

header("Location:thumbs/".$_GET["url"])
?>